const { Schema } = require('./connect')

const UserSchema = new Schema({
  username: String,
  password: String,
  role: {
    type: String,
    default: 1
  },
  avatar: {
    type: String,
    default: "/avatar/default.jpg"
  },
  articleNum: Number,
  commentNum: Number
}, {
  versionKey: false,
  timestamps: {
    createdAt: "created", // 创建时间 
    updatedAt: "updated"
  }
})

UserSchema.post("remove", doc => {
  const userId = doc._id

  const Article = require("../Models/article")
  const Comment = require("../Models/comment")

  Article
    .find({author: userId})
    .then(articles => {
      articles.forEach(article => {
        article.remove()
      })
    })

  Comment
    .find({from: userId})
    .then(comments => {
      comments.forEach(comment => {
        comment.remove()
      })
    })

})


module.exports = UserSchema